import { Message, Loading } from 'element-ui';
import { debounce } from 'lodash'
let mask: any = null;
export default {
    count: 0,
    // 加载遮罩
    loading(message: string) {
        const me = this as any;
        // 计数器加1
        me.count++;
        // console.log('loading，请求总数：', this.count);
        mask = Loading.service({
            lock: true,
            text: message,
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
        });
        // 这样写避免提示信息无法更新
        mask.text = message;
        if (!me.debounceClear) {
            me.debounceClear = debounce(function () {
                mask.close();
            }, 250);
        }
    },
    // 关闭遮罩
    clear() {
        const me = this as any;
        if (me.count > 1) {
            // 当计数器大于1时减1
            me.count--;
            // console.log('loadingend，请求总数减一：', me.count);
        } else {
            // console.log('loadingend，请求总数归0隐藏遮罩')
            // 当计数器等于1时隐藏遮罩
            // 计数器归0
            me.count = 0;
            me.debounceClear();
        }
    },
    // 成功提示
    success(params: any) {
        Message({
            message: params,
            type: 'success'
        });
    },
    // 警告提示
    warning(params: any) {
        Message({
            message: params,
            type: 'warning'
        });
    },
    // 失败提示
    error(params: any) {
        Message({
            message: params,
            type: 'error'
        });
    },
    show(message: any, type: any) {
        Message({
            message: message,
            type: type,
            customClass: "zZindex"
        });
    }
}